package com.mocha.bass.test.minio;

import cn.hutool.core.date.DateUtil;
import io.minio.*;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author songkui
 * @since 2024/7/4 21:03
 */
public class T {
    public static void main(String[] args) throws Exception {
        MinioClient minioClient = MinioClient.builder()
                .endpoint("http://203.25.215.149:30099")
                .credentials("admin", "HYzh248713")
                .build();

        String base64String = readBase64FromFile("d:/test.txt");

        if (base64String != null) {
            base64String = base64String
                    .replace("data:image/png;base64,", "")
                    .replace("data:image/jpeg;base64,", "")
                    .replace("data:image/jpg;base64,", "")
                    .replace(" ", "+");

            System.out.println(base64String);
            // 解码 Base64 字符串为字节数组
            byte[] imageBytes = Base64.getMimeDecoder().decode(base64String);
            // 将字节数组保存为图片文件
//            saveBytesToFile(imageBytes, "d:/output_image2.jpeg");

            InputStream inputStream = new ByteArrayInputStream(imageBytes);

            boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket("bucketName").build());
            if (!isExist) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("bucketName").build());
            }

            minioClient.putObject(buildPutObjectArgs("2024-07-05", buildImportObject("vxxxx"), "xxx.jpeg", inputStream));
        } else {
            System.out.println("Failed to read Base64 string from file.");
        }
    }

    public static String buildImportObject(String random){
        String date = DateUtil.formatDate(new Date());
        return String.format( "vehicle/%s/%s%s", date,  random, ".jpg");
    }

    public static PutObjectArgs buildPutObjectArgs(String bucket, String object, String fileName, InputStream stream) throws IOException {
        Map<String, String> userMetadata = new HashMap<>();
        userMetadata.put("File-Name", fileName);
        userMetadata.put("Creator", "Test");

        return PutObjectArgs.builder()
                .bucket(bucket)
                .object(object)
                .stream(stream, stream.available(), -1) //目前附件比较小 可以这样传入
//                .userMetadata(userMetadata)
                .build();
    }



    // 从文件中读取 Base64 编码的字符串
    private static String readBase64FromFile(String filename) {
        try {
            // 读取文件内容
            byte[] encodedBytes = Files.readAllBytes(Paths.get(filename));
            return new String(encodedBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 将字节数组保存为文件
    private static void saveBytesToFile(byte[] bytes, String filename) {
        try {
            FileOutputStream fos = new FileOutputStream(filename);
            fos.write(bytes);
            fos.close();
            System.out.println("Image saved successfully: " + filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
